	<?php
	defined('BASEPATH') OR exit('No direct script access allowed');

	class Article extends CI_Controller {
		// protected $dictArray = array();
		public function __construct()
    {
      parent::__construct();

      date_default_timezone_set("PRC");
   
            // $this->load->helper('common');
      $this->load->helper('url_helper');
      $this->load->database();
      $this->load->model('article_model');
      $this->load->model('catalog_model');
        

    }

    public function genhtml()
    {
        $this->load->helper('file');
        date_default_timezone_set("PRC");
        $data['config'] = $this->article_model->getconfig();
        $data['title'] = $data['config']->keyword;
        $cataloglist = $this->catalog_model->getAll();
        $catadata = array();
         
        foreach ($cataloglist as $item) {
          if ($item['enable'] != 0)
          {
              $item['items'] = $this->article_model->getindexdata($item['id']);
              $catadata[] = $item;
          }
        }

        // print_r($catadata);

        $data['cataloglist'] = $catadata;
        $this->load->view('templates/pub_header', $data);
        $this->load->view('index', $data);
        $this->load->view('templates/pub_footer');
        
        $html_data = $this->output->get_output();
        write_file('./index77.html',$html_data);
    }

    public function index()
    {
     
        date_default_timezone_set("PRC");
        $data['config'] = $this->article_model->getconfig();
        $data['title'] = $data['config']->keyword;
        $cataloglist = $this->catalog_model->getAll();
        $catadata = array();
         
        foreach ($cataloglist as $item) {
          if ($item['enable'] != 0)
          {
              $item['items'] = $this->article_model->getindexdata($item['id']);
              $catadata[] = $item;
          }
        }

        // print_r($catadata);

        $data['cataloglist'] = $catadata;
        $this->load->view('templates/pub_header', $data);
        $this->load->view('index', $data);
        $this->load->view('templates/pub_footer');
   }

 
   public function detail($id)
    {
  
        date_default_timezone_set("PRC");
        $data['articleInfo'] = $this->article_model->getOne($id);
        $data['title'] = $data['articleInfo']->title;
      
        $data['config'] = $this->article_model->getconfig();
        $data['cataloglist'] = $this->catalog_model->getAll();
      
        $this->load->view('templates/pub_header', $data);
        $this->load->view('pub_detail', $data);
        $this->load->view('templates/pub_footer');
   }
  

 
    public function admin($catalogid=1)
    {
        if (!isset($_SESSION['username'])) {  
            redirect('user/login');  ;  
        }  
  
        date_default_timezone_set("PRC");
        $data['artlist'] = $this->article_model->get($catalogid);
        $data['cataloglist'] = $this->catalog_model->getAll();
     
        $this->load->view('templates/header', $data);
        $this->load->view('article', $data);
        $this->load->view('templates/footer');
   }

   public function add(){
      if (!isset($_SESSION['username']))
      {
          redirect('user/login');
      }


      $this->load->helper('form');
      $this->load->library('form_validation');
      $this->form_validation->set_rules( 'title', '标题', 'required' );
            
      $data['title'] = '添加分类';

      
      if ($this->form_validation->run() === FALSE)
      {
          $data['cataloglist'] = $this->catalog_model->getAll();
     
          $this->load->view('templates/header', $data);
          $this->load->view('articleadd');
          $this->load->view('templates/footer');
          
      }
      else
      {
          $this->article_model->add();
          redirect('article/admin');
      }

    }

   //for ads
    public function status($id,$enable){
     if (!isset($_SESSION['username']))
        {
            redirect('user/login');
        }

        if ($id > 0) {
            $this->article_model->setEnable($id, $enable);
        }
   
        redirect('article/admin');

    }
    
    public function update($id){
       if (!isset($_SESSION['username']))
        {
            redirect('user/login');
        }
        
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules( 'title', '标题', 'required' );
       
         $data['articleInfo'] = $this->article_model->getOne($id);
        if ($this->form_validation->run() === FALSE)
        {
            $data['cataloglist'] = $this->catalog_model->getAll();
     
            $this->load->view('templates/header', $data);
            $this->load->view('articleupdate');
            $this->load->view('templates/footer');
            
        }
        else
        {
            //echo "this.".$userId;
            $this->article_model->update($id);

            redirect('article/admin');
        }
    }


    public function delete($id)
    {
        if (!isset($_SESSION['username'])) {  
            redirect('user/login');  ;  
        }  
        
        // $data['title'] = '删除用户';
        
        if ($id > 0) {
            $this->article_model->delete($id);
        }
   
        redirect('article/admin');

    }


    //for config
    public function configsettings()
    {
  
         if (!isset($_SESSION['username']))
        {
            redirect('user/login');
        }
        
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules( 'website', '网站名称', 'required' );
        
        $data['configInfo'] = $this->article_model->getconfig();
        if ($this->form_validation->run() === FALSE)
        {
            $this->load->view('templates/header', $data);
            $this->load->view('configsettings');
      
           $this->load->view('templates/footer');
            
        }
        else
        {
            //echo "this.".$userId;
            $this->article_model->configsettings();

            $data['configInfo'] = $this->article_model->getconfig();
            $this->load->view('templates/header', $data);
            $this->load->view('configsettings');
      
           $this->load->view('templates/footer');

        }
   }


  }
